package com.vhall.component.controller.interactive;


import com.vhall.component.controller.interactive.facade.DocumentFacade;
import com.vhall.component.entity.interactive.vo.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.VssToken;
import com.vhall.component.plugin.common.support.VssTokenThreadLocal;
import com.vhall.component.plugin.common.utils.DirUtil;
import com.vhall.component.service.interactive.DocumentsRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @author shenqing
 * @date 2021/6/9 17:32
 */
@Slf4j
@VhallComponent
public class DocumentController implements DocumentFacade {

    @Autowired
    private DocumentsRoomService documentsRoomService;

    @Override
    @VssTokenAuth
    public DocumentUploadRsp upload(DocumentUploadReqVO vo) {

        DirUtil.failIfDirectoryTraversal(vo.getDocument().getOriginalFilename());

        if (null == vo.getRoomId() && null == vo.getAccountId()) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        } else if (null != vo.getRoomId() && null != vo.getAccountId()) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        }
        if (vo.getVssToken() != null) {
            VssToken tokenInfo = VssTokenThreadLocal.getToken();
            if (tokenInfo == null) {
                throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
            }
        }
        if (vo.getDocument().isEmpty()) {
            throw new BusinessException(BizErrorCode.COMP_DOC_EMPTY);
        }

        return documentsRoomService.upload(vo);
    }

    @Override
    @TokenAuth
    public DocumentListRspVO list(DocumentListReqVO reqVO) {
        if (null == reqVO.getRoomId() && null == reqVO.getAccountId()) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        }
        return documentsRoomService.list(reqVO);
    }

    @Override
    @VssTokenAuth
    public Boolean delete(DocumentDeleteReqVO reqVO) {
        if (reqVO.getVssToken() != null) {
            VssToken tokenInfo = VssTokenThreadLocal.getToken();
            if (tokenInfo == null) {
                throw new com.vhall.component.framework.common.exception.BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
            }
        }
        return documentsRoomService.delete(reqVO);
    }

}
